Color Puzzle
Volume Number: 4
Issue Number: 4
Column Tag: Macintosh II
The Palette Manager & Color Puzzle
By Steven C. Sheets, Contributing Editor
For the next two months, this column is going to be a little different than the
previous ones. Normally a new area of Mac // programming is picked each month.
That area is discussed, along with it’s implementation and implications, and a sample
program is given that demonstrates the concepts in action.
This month’s (much promised) column is on the Palette Manager, while next
month’s will be on using the Palette Manager to do Palette Animation. In this article,
there will be a explanation of why the Palette Manager is needed and how it works. The
basic Palette Manager calls will be explained. Next month, the sample program,
PalFun, will be taken apart step by step, explaining the Palette Manager Animation
calls and the special effect done using Palette Animation.
Still, every column should have at least one example of code, so presented here is
Color Puzzle. It was created back in the days when there was very little truly useful
color applications or Desk Accessories. Two Mac-Fanatics wanted to show everyone the
real reason they purchase their Mac //s. Their idea of programming was telling this
Author that the Color Puzzle was a needed tool. This Author agrees with them!
RGB vs Pixel Space
So far almost all of the drawing explained in this column has been in RGB space.
A program picks a specific color, consisting of an 8 bit Red component, an 8 bit Green
component and an 8 bit Blue component. The program then draws with this color,
using Color Quickdraw commands, to a specific Color Graphics Device (ex. Apple’s Mac
// Video Card/Monitor). While most Graphics Devices can display any possible RGB
color, most Devices can only display a finite number of colors at any time. This is due
to the way the memory of the Mac // Video Devices (ie. Screen) is structured.
Every pixel on the screen has an associated spot in memory. On the older
non-Color Macs, this spot in memory was a single bit that designated if the pixel was
white or black. On the color Mac //, this spot in memory is a variable size, unsigned
integer. The integer contains an index number. That number corresponds to a table of
RGB colors (called a Color Look Up Table or CLUT) that the Video card maintains and
uses to physically display the screen. If a pixel has a index of 3 in memory, that same
pixel is displayed on the Video Card/Monitor using the 3rd color in the Video Card’s
CLUT. Changing the pixel index, changes the pixel color on the screen. Color
Quickdraw does it’s drawing by manipulation of each pixel’s index.
Obviously the number of colors most card can display is dependent on the amount
of memory used for each pixels; 2 to the power of the number of bits per pixels is
equal to the number of colors that can be displayed at one time. For example, the
initial Apple Video Card has 4 bits per pixels and can only display 16 colors at one
time. If someone wants to upgrade the memory on the card using Apple’s Video
Expansion Kit to 8 bits per pixel, then the card can display up to 256 colors at one
time. 8 Bits per pixel (256 Colors) is currently the most common configuration of
Mac // Graphic Cards, though 24 Bit Cards may be just around the corner.
Fig. 1 Our color puzzle in black and white! Pressing the option key brings up the color
chooser.
The decision of what colors are to be physically displayed on a card and what RGB
values map into which physical color is handled by the Color Manager (not the
Quickdraw Manager). When the Mac // is initially booted, the Color Manager
normally sets the colors of each Graphics Device to commonly used colors. The Mac
//’s default color environment (ie. settings of the CLUT) are sometimes called the
“Apple System Colors”. When Color Quickdraw needs to know what index corresponds
to the RGB value it wants to draw with, it calls the Color Manager. The Color Manager
checks the current status of the Video Card’s CLUT and returns an index value. This is
the index of the RGB value closest to the RGB color Color Quickdraw wants to draw
with. There may not be a RGB color in the Video Card’s CLUT anywhere close to the
requested color. The Color Manager returns the best choise given the current status of
the CLUT. For many applications, best choice is good enough. However it is not hard to
think of applications that need exact control of the colors used. If the program is an
elaborate Drawing or Painting program, the user may want to control the colors
himself, allowing more greens for a landscrape, or more flesh tones for a portrait. If
the program is a game, the various figures and images may have to be in a specific
color. If the program is connected to a video input scanner, the program may want to
set the colors of the card to match the colors used by the scanner. The Color Manager
allows direct manipulation of each pixel’s colors.
Color Manager vs Palette Manager
After praising the Color Manager for what it can do, the next statement may sound
a little strange; NEVER use the Color Manager! Instead use the Palette Manager. The
Color Manager would be adequate to use if all programs were written to be the only
thing running on a specific system configuration. This is almost never the case. Most
programs have to be written to run on any number or size of video cards. Other code
segments may be running at the same time that require different colors. Obviously
programs under Multifinder needs to share the colors. Even Desk Accessories may
need specific colors. Who decides which colors are needed? More importantly, when
there is a limited number of bits on the video card, who decided which colors are safe
not to include (more than one code segment may want the same color). If video card’s
colors change, how does the rest of the system handle this? The Palette Manager is the
solution for these problems.
When using the Color Manager, the Manager is manipulating the entire Color
Table on a Specific Card (cumbersome). When using the Palette Manager, the Manager
is concerned with groups of colors (ie. a Palette) that each specific window needs to
display. The Palette Manager calls the Color Manager to select the best choice of colors
for each video card (no matter it’s size) that would make all the Palettes happy.
To paraphrase a quote, “You can not make all the palettes happy all the time”.
However, a program can make the user happy all the time. Each Palette is associated
with a given window. It is logical to give the top most Window (and it’s associated
Palette) the highest priority. This is the window the user is currently looking at. If
the top most Palette is smaller than the number of colors on a card, other Palettes are
used to select the remaining colors. If there are not enough colors for all the Palettes,